package com.atguigu.demojuc.chap06;

import java.util.concurrent.*;

class MyRunnable implements Runnable{
    private int param;
    public MyRunnable(int param){
        this.param=param;
    }
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"Runnable...."+param);

    }
}
public class CustomizeThreadPoolDemo {
    public static void main(String[] args) {
        //自定义线程连接池
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(2,5,2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3), Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());//丢弃任务并抛出异常
                //new ThreadPoolExecutor.CallerRunsPolicy() //由调用线程处理该任务，谁调用谁处理
                //new ThreadPoolExecutor.DiscardOldestPolicy() //丢弃队列中等待最久的任务，添加新任务
                //new ThreadPoolExecutor.DiscardPolicy() //也是丢弃任务，但是不抛出异常。
                /*new RejectedExecutionHandler() {
                    @Override
                    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                        System.out.println("自定义拒绝策略");
                    }
                }*/
        try {
            for (int i = 0; i < 8; i++) {
                threadPoolExecutor.execute(() -> {
                    System.out.println(Thread.currentThread().getName()+"执行了业务逻辑");
                });
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        finally {
            threadPoolExecutor.shutdown();
        }
    }
}
